package com.example.learn.service;

import com.example.learn.config.listener.MyEvent;
import com.example.learn.domain.Optionaler;
import com.example.learn.entity.Account;
import com.example.learn.entity.User;
import com.example.learn.mapper.AccountMapper;
import com.example.learn.mapper.UserMapper;
import com.example.learn.token.CurrentUser;
import com.example.learn.utile.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    AccountMapper accountMapper;

    public boolean addUser(User user) {
        User user2 = userMapper.selectByName(user.getName());
        if (user2 != null) {
            return false;
        }
        user.setId(UUID.randomUUID().toString());
        user.setCreatetime(TimeUtil.getCurrentTime());
        userMapper.insert(user);
        return true;
    }

    public User login(String name, String password) {
        User user = userMapper.selectByNameAndPassword(name, password);
        return user;
    }

    public User findUserById(String userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 发布事件
     *
     * @return
     */
    public User getUser2() {
        User user = new User();
        user.setPassword("1111");
        user.setName("你好");
        // 发布事件
        MyEvent event = new MyEvent(this, user);
        applicationContext.publishEvent(event);
        return user;
    }


    public boolean getName(String name) {
        User user2 = userMapper.selectByName(name);
        if (user2 != null) {
            return false;
        }
        return true;
    }

    public boolean updateUser(User user) {
        user.setUpdatetime(TimeUtil.getCurrentTime());
        int update = userMapper.updateByPrimaryKey(user);
        if (update == 1) {
            return true;
        } else {
            return false;
        }
    }

    public User getUserByName(String name) {
        User user = userMapper.selectByName(name.replace("\"", ""));


        return user;
    }

    public String addfamily(String name, @CurrentUser User user) {
        User user2 = userMapper.selectByName(name.replace("\"", ""));
        if(user2==null){
            return "不存在此用户,请检查登录账号是否出错";
        }
        if(StringUtils.isEmpty(name)){
            return "账号不能为空";
        }
        Account accountOld = accountMapper.getByAccountorName(name, user.getName());
        if (accountOld != null) {
            return "家庭成员已经在列表";
        }
        String id = UUID.randomUUID().toString();
        Account account = new Account();
        account.setId(id);
        account.setAccount(name);
        account.setName(user.getName());
        account.setDate(TimeUtil.getCurrentDate());
        int i = accountMapper.insert(account);
        return "ok";
    }

    public List<User> getFamilyName(@CurrentUser User user) {
        List<Account> accountOlds = accountMapper.getFamilyName(user.getName());
        List<User> userList = new ArrayList<>();
        Set<String> set=new HashSet<String>();
        for (Account aco:accountOlds
             ) {
            set.add(aco.getName());
            set.add(aco.getAccount());
        }
        set.stream().forEach(account ->
                userList.add(userMapper.selectByName(account))
        );

        return userList;
    }

    public String deleteFamilyName(String name,User user) {
        Account account= accountMapper.getByAccountorName(name,user.getName());
        accountMapper.deleteByPrimaryKey(account.getId());
        return "ok";
    }

    public List<Optionaler> getoptions(@CurrentUser User user) {
        List<Account> accountOlds = accountMapper.getFamilyName(user.getName());
        List<Optionaler> userList = new ArrayList<>();
        Set<String> sets=new HashSet<String>();
        for (Account aco:accountOlds
        ) {
            sets.add(aco.getName());
            sets.add(aco.getAccount());
        }
        for (String set:sets
             ) {
         User   account= userMapper.selectByName(set);
            Optionaler option=new Optionaler();
            option.setId(account.getId());
            option.setLabel(account.getTruename());
            option.setValue(account.getName());
            userList.add(option);
        }
        Optionaler option2=new Optionaler();
        option2.setLabel("家庭");
        option2.setValue("family");
        userList.add(option2);
        return userList;
    }
}
